(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "react", "maishu-ui-toolkit"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const React = require("react");
    const ui = require("maishu-ui-toolkit");
    class ItemDialog extends React.Component {
        constructor(props) {
            super(props);
            this.confirmButtonText = "保存";
            this.cancelButtonText = "取消";
            this.state = { item: {} };
            let render = this.render;
            console.assert(render != null);
            this.render = () => {
                let { title, item } = this.state || {};
                if (item == null)
                    item = {};
                return React.createElement("div", { className: "modal fade", ref: e => this.element = e || this.element },
                    React.createElement("div", { className: "modal-dialog" },
                        React.createElement("div", { className: "modal-content" },
                            React.createElement("div", { className: "modal-header" },
                                React.createElement("button", { type: "button", className: "close", "data-dismiss": "modal" },
                                    React.createElement("span", { "aria-hidden": "true" }, "\u00D7"),
                                    React.createElement("span", { className: "sr-only" }, "Close")),
                                React.createElement("h4", { className: "modal-title" }, title || "")),
                            React.createElement("div", { className: "modal-body" }, render.bind(this)()),
                            React.createElement("div", { className: "modal-footer", style: { marginTop: 0 } },
                                React.createElement("button", { type: "button", className: "btn btn-default", "data-dismiss": "modal" }, this.cancelButtonText),
                                React.createElement("button", { type: "button", className: "btn btn-primary", onClick: () => this.confirm() }, this.confirmButtonText)))));
            };
        }
        confirm() {
            if (this.validator && !this.validator.check())
                return;
            let { item } = this.state;
            Object.assign(this.item, item);
            ui.hideDialog(this.element);
            if (this.confirmCallback)
                this.confirmCallback(item);
        }
        show(arg1, arg2, arg3) {
            let item, title, confirm;
            if (typeof arg1 == "string") {
                title = arg1;
                confirm = arg2;
            }
            else {
                item = arg1;
                title = arg2;
                confirm = arg3;
            }
            if (this.validator)
                this.validator.clearErrors();
            this.item = item;
            this.confirmCallback = confirm;
            this.setState({ item: Object.assign({}, item), title }, () => {
                ui.showDialog(this.element);
            });
        }
    }
    exports.ItemDialog = ItemDialog;
});
// export function createItemDialog<T, O>(args: {
//     createElement: (item: T, self: React.Component<{}, { item: T, title?: string }>, options?: O) => JSX.Element,
//     confirm?: (item: T) => void,
// }) {
//     class ItemDialog extends React.Component<{}, { item: T, title?: string }> {
//         static options: O;
//         static item: T;
//         confirm() {
//             if (!args.confirm)
//                 return;
//             let { item } = this.state;
//             Object.assign(ItemDialog.item, item);
//             args.confirm(ItemDialog.item);
//         }
//         render() {
//             let { item, title } = this.state || {} as this["state"];
//             if (item == null)
//                 return <></>;
//             return <div className="modal-dialog">
//                 <div className="modal-content">
//                     <div className="modal-header">
//                         <button type="button" className="close" data-dismiss="modal">
//                             <span aria-hidden="true">&times;</span>
//                             <span className="sr-only">Close</span>
//                         </button>
//                         <h4 className="modal-title">{title || ""}</h4>
//                     </div>
//                     <div className="modal-body">
//                         {args.createElement.bind(this)(item, ItemDialog.options)}
//                     </div>
//                     <div className="modal-footer" style={{ marginTop: 0 }}>
//                         <button type="button" className="btn btn-default" data-dismiss="modal">取消</button>
//                         <button type="button" className="btn btn-primary"
//                             onClick={() => this.confirm()}>确定</button>
//                     </div>
//                 </div>
//             </div>
//         }
//     }
//     let dialogElement = this.dialogElement = document.createElement("div");
//     dialogElement.className = "modal fade";
//     document.body.appendChild(dialogElement);
//     let instance: ItemDialog;
//     ReactDOM.render(<ItemDialog ref={e => instance = e || instance} />, dialogElement);
//     function show(item: T, title: string, options?: O) {
//         ItemDialog.options = options;
//         ItemDialog.item = item;
//         instance.setState({ item: Object.assign({}, item), title });
//         ui.showDialog(dialogElement);
//     }
//     return { show };
// }
